package org.geektimes.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @FileName: PhoneValidator
 * @Description: 手机号校验器
 * @Author: MaWenHao
 * @Created: 2021/3/10 - 下午9:55
 */
public class PhoneValidator implements ConstraintValidator<PhoneValid, String> {

    private Pattern pattern;
    @Override
    public void initialize(PhoneValid constraintAnnotation) {
        pattern = Pattern.compile(constraintAnnotation.value());
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        constraintValidatorContext.getDefaultConstraintMessageTemplate();
        return pattern.matcher(s).matches();
    }
}
